import { createWebHistory, createRouter, RouteOption } from 'vue-router';
/* Layout */
import Layout from '@/layout/index.vue';

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
 *    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
 *    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
 *    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
 *    breadcrumb: false             // 如果设置为false，则不会在breadcrumb面包屑中显示
 *    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
 *  }
 */

// 公共路由
export const constantRoutes: RouteOption[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/index.vue'),
        name: 'Index',
        meta: { title: '首页', icon: 'menu-home', affix: true }
      }
    ]
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/social-callback',
    hidden: true,
    component: () => import('@/layout/components/SocialCallback/index.vue')
  },
  {
    path: '/login',
    component: () => import('@/views/login.vue'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register.vue'),
    hidden: true
  },
  {
    path: '/cd/share/:code',
    name: 'ShareVisit',
    component: () => import('@/views/cd/share-visit.vue'),
    hidden: true,
    meta: { title: '分享访问', hidden: true }
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404.vue'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401.vue'),
    hidden: true
  },
  {
    path: '/online',
    component: Layout,
    children: [
      {
        path: 'test/:tableId(\\d+)/:tableName(.*)',
        component: () => import('@/views/online/test/index.vue'),
        name: 'OnlineTest',
        props: true,
        meta: { title: '功能测试', icon: '', noCache: true },
        hidden: true
      },
      {
        path: 'db',
        component: () => import('@/views/online/db/index.vue'),
        name: 'TableDesigner',
        meta: { title: '表设计器', icon: '', noCache: true },
        hidden: true
      },
      {
        path: 'db/list',
        component: () => import('@/views/online/db/list.vue'),
        name: 'TableDesignList',
        meta: { title: '表设计列表', icon: '', noCache: true },
        hidden: true
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/system/user/profile/index.vue'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  }
];

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes: RouteOption[] = [
  {
    path: '/system/user-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:user:edit'],
    children: [
      {
        path: 'index/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole.vue'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user', icon: '' }
      }
    ]
  },
  {
    path: '/system/role-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'index/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser.vue'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/system/role', icon: '' }
      }
    ]
  },
  {
    path: '/system/dict-data',
    component: Layout,
    hidden: true,
    permissions: ['system:dict:list'],
    children: [
      {
        path: 'index/:dictId(\\d+)',
        component: () => import('@/views/system/dict/data.vue'),
        name: 'Data',
        meta: { title: '字典数据', activeMenu: '/system/dict', icon: '' }
      }
    ]
  },
  {
    path: '/system/oss-config',
    component: Layout,
    hidden: true,
    permissions: ['system:ossConfig:list'],
    children: [
      {
        path: 'index',
        component: () => import('@/views/system/oss/config.vue'),
        name: 'OssConfig',
        meta: { title: '配置管理', activeMenu: '/system/oss', icon: '' }
      }
    ]
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    hidden: true,
    permissions: ['tool:gen:edit'],
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable.vue'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen', icon: '' }
      }
    ]
  },
  {
    path: '/online',
    component: Layout,
    hidden: true,
    permissions: ['online:test:list'],
    children: [
      {
        path: 'edit/:tableId(\\d+)',
        component: () => import('@/views/online/edit/index.vue'),
        name: 'OnlineEdit',
        meta: { title: '修改配置', activeMenu: '/online/edit', icon: '' }
      },
      {
        path: 'db',
        component: () => import('@/views/online/db/index.vue'),
        name: 'TableDesigner',
        meta: { title: '表设计器', activeMenu: '/online/db', icon: '' }
      }
    ]
  },
  {
    path: '/workflow/process',
    component: Layout,
    hidden: true,
    permissions: ['workflow:process:query'],
    children: [
      {
        path: 'start/:deployId([\\w|\\-]+)',
        component: () => import('@/views/workflow/work/start.vue'),
        name: 'WorkStart',
        meta: { title: '发起流程', activeMenu: '/workflow/process', icon: '' }
      },
      {
        path: 'detail/:procInsId([\\w|\\-]+)',
        component: () => import('@/views/workflow/work/detail.vue'),
        name: 'WorkDetail',
        meta: { title: '流程详情', activeMenu: '/work/own', icon: '' }
      }
    ]
  },
  {
    path: '/workflow/nform',
    component: Layout,
    hidden: true,
    permissions: ['workflow:nform:list'],
    children: [
      {
        path: 'test',
        component: () => import('@/views/workflow/nform/test.vue'),
        name: 'NformTest',
        meta: { title: '表单设计器测试', activeMenu: '/workflow/nform', icon: '' }
      }
    ]
  },
  {
    path: '/pm',
    component: Layout,
    redirect: '/pm/dashboard',
    name: 'ProjectManagement',
    meta: {
      title: '项目管理',
      icon: 'project'
    },
    children: [
      {
        path: 'dashboard',
        name: 'PmDashboard',
        component: () => import('@/views/pm/dashboard/index.vue'),
        meta: { title: '项目仪表板', icon: 'dashboard' }
      },
      {
        path: 'project',
        name: 'PmProject',
        component: () => import('@/views/pm/project/index.vue'),
        meta: { title: '项目管理', icon: 'project' }
      },
      {
        path: 'project/member',
        name: 'PmProjectMember',
        component: () => import('@/views/pm/project/member/index.vue'),
        meta: { title: '项目成员', icon: 'user' }
      },
      {
        path: 'task',
        name: 'PmTask',
        component: () => import('@/views/pm/task/index.vue'),
        meta: { title: '任务管理', icon: 'task' }
      },
      {
        path: 'team',
        name: 'PmTeam',
        component: () => import('@/views/pm/team/index.vue'),
        meta: { title: '团队管理', icon: 'peoples' }
      },
      {
        path: 'team/member',
        name: 'PmTeamMember',
        component: () => import('@/views/pm/team/member/index.vue'),
        meta: { title: '团队成员', icon: 'user' }
      },
      {
        path: 'document',
        name: 'PmDocument',
        component: () => import('@/views/pm/document/index.vue'),
        meta: { title: '文档管理', icon: 'document' }
      },
      {
        path: 'time',
        name: 'PmTime',
        component: () => import('@/views/pm/time/index.vue'),
        meta: { title: '时间跟踪', icon: 'time' }
      },
      {
        path: 'report',
        name: 'PmReport',
        component: () => import('@/views/pm/report/index.vue'),
        meta: { title: '报表统计', icon: 'chart' }
      }
    ]
  },
  {
    path: '/erp',
    component: Layout,
    redirect: '/erp/material',
    name: 'Erp',
    meta: {
      title: '进销存',
      icon: 'project'
    },
    children: [
      {
        path: 'material',
        name: 'ErpMaterial',
        component: () => import('@/views/erp/material/index.vue'),
        meta: { title: '物料管理', icon: 'folder' }
      },
      {
        path: 'warehouse',
        name: 'ErpWarehouse',
        component: () => import('@/views/erp/warehouse/index.vue'),
        meta: { title: '仓库管理', icon: 'folder' }
      },
      {
        path: 'party/supplier',
        name: 'ErpSupplier',
        component: () => import('@/views/erp/party/supplier/index.vue'),
        meta: { title: '供应商管理', icon: 'folder' }
      },
      {
        path: 'party/customer',
        name: 'ErpCustomer',
        component: () => import('@/views/erp/party/customer/index.vue'),
        meta: { title: '客户管理', icon: 'folder' }
      },
      {
        path: 'po-in',
        name: 'ErpPoIn',
        component: () => import('@/views/erp/bill/list.vue'),
        props: { billType: 'PO_IN' },
        meta: { title: '采购入库', icon: 'folder' }
      },
      {
        path: 'po-in/edit',
        name: 'ErpPoInEdit',
        component: () => import('@/views/erp/bill/edit.vue'),
        props: () => ({ id: undefined, billType: 'PO_IN' }),
        meta: { title: '采购入库编辑', hidden: true }
      },
      {
        path: 'po-in/edit/:id',
        name: 'ErpPoInEditWithId',
        component: () => import('@/views/erp/bill/edit.vue'),
        props: route => ({ id: route.params.id as string, billType: 'PO_IN' }),
        meta: { title: '采购入库编辑', hidden: true }
      },
      {
        path: 'so-out',
        name: 'ErpSoOut',
        component: () => import('@/views/erp/bill/list.vue'),
        props: { billType: 'SO_OUT' },
        meta: { title: '销售出库', icon: 'folder' }
      },
      {
        path: 'so-out/edit',
        name: 'ErpSoOutEdit',
        component: () => import('@/views/erp/bill/edit.vue'),
        props: () => ({ id: undefined, billType: 'SO_OUT' }),
        meta: { title: '销售出库编辑', hidden: true }
      },
      {
        path: 'so-out/edit/:id',
        name: 'ErpSoOutEditWithId',
        component: () => import('@/views/erp/bill/edit.vue'),
        props: route => ({ id: route.params.id as string, billType: 'SO_OUT' }),
        meta: { title: '销售出库编辑', hidden: true }
      },
      {
        path: 'price',
        name: 'ErpPrice',
        component: () => import('@/views/erp/price/index.vue'),
        meta: { title: '价目表', icon: 'folder' }
      },
      {
        path: 'account',
        name: 'ErpAccount',
        component: () => import('@/views/erp/account/index.vue'),
        meta: { title: '会计科目', icon: 'folder' }
      },
      {
        path: 'account/config',
        name: 'ErpAccountConfig',
        component: () => import('@/views/erp/account/config/index.vue'),
        meta: { title: '科目配置', icon: 'folder' }
      },
      {
        path: 'report/inventory',
        name: 'ErpInventoryReport',
        component: () => import('@/views/erp/report/inventory/index.vue'),
        meta: { title: '库存报表', icon: 'folder' }
      },
      {
        path: 'report/finance',
        name: 'ErpFinanceReport',
        component: () => import('@/views/erp/report/finance/index.vue'),
        meta: { title: '财务报表', icon: 'folder' }
      }
    ]
  },
  {
    path: '/cd',
    component: Layout,
    redirect: '/cd/index',
    name: 'CloudDrive',
    meta: {
      title: '云网盘',
      icon: 'cloud-drive'
    },
    children: [
      {
        path: 'index',
        name: 'CdIndex',
        component: () => import('@/views/cd/index.vue'),
        meta: { title: '我的文件', icon: 'folder' }
      },
      {
        path: 'share',
        name: 'CdShare',
        component: () => import('@/views/cd/share/index.vue'),
        meta: { title: '分享管理', icon: 'share' }
      },
      {
        path: 'admin',
        name: 'CdAdmin',
        component: () => import('@/views/cd/admin/index.vue'),
        meta: { title: '存储管理', icon: 'setting' }
      }
    ]
  }
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_CONTEXT_PATH),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

export default router;
